<?php

ob_start();
session_start();

class MemberController extends Controller {

    public function actionCheckLogin() {
        $attributes = array();
        $attributes["userlog"] = $_POST["user_username"];
        $attributes["passlog"] = $_POST["user_password"];
        //   $attributes["active_works"] = "working";

        $model = Userlog::model()->findByAttributes($attributes);

        if (!empty($model)) {
            Yii::app()->session["user_id"] = $model->id;
            Yii::app()->session["user_name"] = $model->f_name;
            Yii::app()->session["user_surname"] = $model->l_name;
            Yii::app()->session["user_status"] = $model->status;
            
            $this->redirect(array("home"));
        } else {    
                 Yii::app()->user->setFlash('error', "คุณกรอกชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง!");
                                                                $this->redirect(Yii::app()->homeUrl);
                                                                $this->refresh();

               // $this->redirect(Yii::app()->homeUrl);
            }
    }

    public function actionHome() {
        $this->render("home");
    }

    public function actionLogout() {
        unset(Yii::app()->session["user_id"]);
        unset(Yii::app()->session["user_name"]);
        unset(Yii::app()->session["user_surname"]);
        unset(Yii::app()->session["user_status"]);

        $this->redirect(Yii::app()->homeUrl);
    }

    public function actionProfile() {
        $id = Yii::app()->session["user_id"];

        $model = new Userlog();
        $criteria = new CDbCriteria();
        $criteria->compare("id", "$id");

        $user = new CActiveDataProvider($model, array(
            "criteria" => $criteria
        ));

        $this->render("Profile", array(
            "user" => $user
        ));
    }

    function actionEditProfile($id = null) {
        $user = new Userlog();
        
        //save
        if(!empty($_POST)) {
            $id = $_POST["Userlog"]["id"];
            
            if (!empty($id)) {
                $user = Userlog::model()->findByPk($id);
                
            }
            $user->_attributes = $_POST["Userlog"];
            if ($user->save()) {
                $this->redirect(array("//Config/Employee"));
            }
        }

        //render
        if (!empty($id)) {
            $user = Userlog::model()->findByPk($id);
        }
       
        $this->render("EditProfile", array(
            "user" => $user
        ));
    }
    
      

     function actionEditProfileUser($id = null) {
        $user = new Userlog();
        
        //save
        if(!empty($_POST)) {
            $id = $_POST["Userlog"]["id"];
            
            if (!empty($id)) {
                $user = Userlog::model()->findByPk($id);
                
            }
            $user->_attributes = $_POST["Userlog"];
            if ($user->save()) {
                $this->redirect(array("//Member/Profile"));
            }
        }

        //render
        if (!empty($id)) {
            $user = Userlog::model()->findByPk($id);
        }
       
        $this->render("EditProfileUser", array(
            "user" => $user
        ));
    }

}
